package com.gitee.feizns.itrip.personal.search.service.impl;

import com.gitee.feizns.itrip.personal.beans.vo.impl.PageVoForSpringData;
import com.gitee.feizns.itrip.personal.search.entity.ao.ForAcceptSearchItripHotelPage;
import com.gitee.feizns.itrip.personal.search.entity.vo.HotelData;
import com.gitee.feizns.itrip.personal.search.service.HotelService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;

/**
 * @author feizns
 * @since 2019/1/4 0004
 */
@Service
public class HotelServiceImpl implements HotelService {

    public static final String QUERY_COLLECTION = "hotel";

    @Autowired
    private SolrTemplate solrTemplate;

    @Override
    public com.gitee.feizns.itrip.personal.beans.vo.Page<HotelData> search(ForAcceptSearchItripHotelPage condition) {

        Query query = condition != null ? convertForSolr(condition) : new SimpleQuery("*:*");

        Page<HotelData> page = solrTemplate.queryForPage(QUERY_COLLECTION, query, HotelData.class);

        return new PageVoForSpringData<>(page);
    }

    //构造solr查询条件
    private static Query convertForSolr(@NonNull ForAcceptSearchItripHotelPage condition) {

        Query result = StringUtils.hasText(condition.getKeywords()) ? new SimpleQuery("keywords:" + condition.getKeywords()) : new SimpleQuery("*:*");

        if ( StringUtils.hasText(condition.getAscSort()) )
            result.addSort(new Sort(Sort.Direction.ASC, condition.getAscSort()));

        if ( StringUtils.hasText(condition.getDescSort()) )
            result.addSort(new Sort(Sort.Direction.DESC, condition.getAscSort()));

        if ( StringUtils.hasText(condition.getHotelLevel()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("hotelLevel:{0}", condition.getHotelLevel())));

        if ( StringUtils.hasText(condition.getMaxPrice()) && StringUtils.hasText(condition.getMinPrice()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("minPrice:[{0} TO {1}]", condition.getMinPrice(), condition.getMaxPrice())));
        else if ( StringUtils.hasText(condition.getMinPrice()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("minPrice:[{0} TO *]", condition.getMinPrice())));
        else if ( StringUtils.hasText(condition.getMaxPrice()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("maxPrice:[* TO {0}]", condition.getMaxPrice())));

        if ( StringUtils.hasText(condition.getDestination()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("destination:{0}", condition.getDestination())));

        if ( StringUtils.hasText(condition.getFeatureIds()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("featureIds:{0}", condition.getFeatureIds())));

        if ( StringUtils.hasText(condition.getTradeAreaIds()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("tradeAreaIds:{0}", condition.getTradeAreaIds())));

        if ( StringUtils.hasText(condition.getCityId()) )
            result.addFilterQuery(new SimpleQuery(MessageFormat.format("cityId:{0}", condition.getCityId())));

        int curt = 0;   //第一页

        int size = 10;  //每页大小

        if ( condition.getPageNo() != null && condition.getPageNo() > 0 )
            curt = condition.getPageNo() - 1;

        if ( condition.getPageSize() != null && condition.getPageSize() > 0 )
            size = condition.getPageSize();

        result.setPageRequest(new PageRequest(curt, size));

        return result;
    }

}
